package logic

import (
	"context"

	"backend/apps/book/api/internal/svc"
	"backend/apps/book/api/internal/types"
	"backend/apps/book/proto"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetAuthorLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetAuthorLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAuthorLogic {
	return &GetAuthorLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetAuthorLogic) GetAuthor(req *types.IdRequest) (resp *types.Author, err error) {
	result, err := l.svcCtx.BookRpc.GetAuthor(l.ctx, &proto.IdRequest{Id: req.Id})
	if err != nil {
		return nil, err
	}

	return &types.Author{
		Id:           result.Id,
		Name:         result.Name,
		Avatar_url:   result.AvatarUrl,
		Introduction: result.Introduction,
	}, nil
}
